<template>
    <dzs-dialog :title="title" :isShow="isShow" @close="onCancel" @open="onOpen">
        <dzs-form :loadingOption="formInfo" :status="status" ref="myForm" v-model:value="formData"
            :options="formOptions" @onCancel="onCancel" @onSubmit="onSubmit" @change="onChange">
        </dzs-form>
    </dzs-dialog>
</template>
<script setup lang="ts">
import { ref, computed , onMounted} from "vue";
import type { dzsFormProps } from "@/components/dzs-form/@types";
import type { dzsFormInstance } from "@/components/dzs-form";
import type { formData } from "../@types";
import { ElNotification } from 'element-plus';
import type { CommonResponse } from "@/@types";
import { addBanner, updataBanner } from "../api/api";
import { uploadImg } from "@/api/api";
import { getArticle } from "../../article-manage/api/api";
import type { tableItem } from "../@types";
const emit = defineEmits(['cancel', 'update:isShow', 'onHoldSearch'])
const props = defineProps<{
    dialogData?: tableItem,
    status: string,
    isShow: boolean
}>()

const myForm = ref<dzsFormInstance>();
const formData = ref<formData>({
    ID: "",
    title: "",
    ImgUrl: "",
    articleID: "",
    path: "",
    mpPath: "",
    is_text: "0",
    is_show: 1
})

const formInfo = ref({
    loading: false,
})

const articleList = ref<Array<{ label: string, value: string }>>([])

const title = computed(() => {
    return props.status === "add" ? "添加轮播图" : props.status === "edit" ? "编辑轮播图" : "查看轮播图详情"
})

onMounted(() => {
    onGetArticle()
})

const formOptions = computed<dzsFormProps['formOptions']>(() => {
    return {
        formProps: {
            "labelWidth": "140px"
        },
        formItem: [
            {
                label: "ID",
                type: "input",
                key: "ID",
                isHidden: true,
            },
            {
                label: "显示图片",
                type: "uploadImg",
                key: "ImgUrl",
                props: {
                    openUploadFun: true,
                    uploadFun: (file: any, success: Function) => {
                        let formData = new FormData();
                        formData.append('file', file.raw);
                        uploadImg(formData).then((res: any) => {
                            success({ path: res.data.url }, {
                                ...file,
                                url: res.data.path +  res.data.url
                            })
                        })
                    }
                }
            },
            {
                label: "文字显示内容",
                type: "input",
                key: "title",
                props: {
                    placeholder: "请输入文字显示内容"
                },
                rules: [
                    { required: true, message: '请输入文字显示内容', trigger: 'blur' },
                ]
            },
            {
                label: "关联文章",
                type: "select",
                key: "articleID",
                children: articleList.value,
                props : {
                    filterable: true,
                },
            },
            {
                label: "跳转地址",
                type: "input",
                key: "path",
                props: {
                    placeholder: "请输入跳转地址"
                }
            },
            {
                label: "小程序跳转地址",
                type: "input",
                key: "mpPath",
                props: {
                    placeholder: "请输入小程序跳转地址"
                }
            },
            {
                label: "是否纯文本",
                type: "switch",
                key: "is_text",
                props: {
                    "active-value": "1",
                    "inactive-value": "0"
                }
            },
            {
                label: "是否展示",
                type: "switch",
                key: "is_show",
                props: {
                    "active-value": 1,
                    "inactive-value": 0
                }
            },
        ]
    }
})

const onCancel = () => {
    myForm.value?.onCancel<formData>(false, {
        success: (res) => {
            formData.value = res
        }
    })
    emit('cancel');
    emit('update:isShow', false);
}


const onOpen = () => {
    if (props.dialogData?.ID) {
        formData.value = props.dialogData
    }
}

const onSubmit:dzsFormProps['onSubmit'] = () => {
    formInfo.value.loading = true;
    if (Array.isArray(formData.value.ImgUrl) && formData.value.ImgUrl.length > 0) {
        formData.value.ImgUrl = formData.value.ImgUrl[0].path;
    }
    if (typeof formData.value.ImgUrl != 'string') {
        formData.value.ImgUrl = '';
    }
    if (props.status === "edit") {
        updataBanner({
            ...formData.value
        }).then(res => {
            formInfo.value.loading = false;
            ElNotification({
                title: '成功',
                message: "提交成功",
                type: 'success'
            })
            emit("onHoldSearch");
            onCancel();
        }).catch(() => {
            formInfo.value.loading = false;
        })
        return;
    }
    addBanner({
        ...formData.value
    }).then(res => {
        formInfo.value.loading = false;
        ElNotification({
            title: '成功',
            message: "提交成功",
            type: 'success'
        })
        emit("onHoldSearch");
        onCancel();
    }).catch(() => {
        formInfo.value.loading = false;
    })
}

const onGetArticle = () =>{
    getArticle({
        isAll : true
    }).then(res => {
        const result: CommonResponse<tableItem[]> = res;
        articleList.value = result.data.data.map((item: tableItem) => {
            return {
                label: item.title,
                value: item.ID
            }
        })
    })
}

const onChange : dzsFormProps<formData>['onChange'] = (e) => {
    if(e.key == 'articleID'){
        formData.value.path = `/details/${e.value}`;
        formData.value.mpPath = `/pages/details/details?id=${e.value}`;
    }
}
</script>
<style scoped></style>